var pageState = {
	detail1 : false,
	detail2 : false,
	detail3 : false
};
var progress = $("input[name=progress]").val();
var orderID = $("input[name=detailOrderID]").val();
var app = angular.module('orderDetailApp', [ 'ngRoute' ]);
app.config([ '$routeProvider', function($routeProvider) {
	$routeProvider.when('/', {
		templateUrl : 'order_detail1.html',
        controller: 'detail1Controller'
	}).when('/progress', {
		templateUrl : (progress == '0' ? 'order_detail2.html' : 'order_detail_done.html'),
        controller: 'detail2Controller'
	}).when('/fee', {
		templateUrl : 'order_detail3.html',
        controller: 'detail3Controller'
	}).otherwise({
		redirectTo : '/'
	});
} ]);
app.controller('orderDetailController', function($scope, $http) {
	
	$scope.titleClass = [ {
		"li" : "w33 rline",
		"span" : "tabfouse"
	}, {
		"li" : "w33",
		"span" : ""
	}, {
		"li" : "w33",
		"span" : ""
	} ];

	$scope.statusLabel = [ "进行中", "已完成" ];
	$scope.orderDetail = {
			"orderNo" : "",
			"createTime" : "",
			"poster" : "",
			"name" : "",
			"dept" : "",
			"instrumentList": [],
			"sum" : ""
		};
	
	var query = function(){

		$http({
	        method: 'POST',
	        url: '../query.do',
	        data: { "queryID": "home_service.S002", "condition": [orderID] },  
	        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },  
           	transformRequest: function(obj) {
            	var str = [];
            	for (var s in obj) {
                	str.push(encodeURIComponent(s) + "=" + encodeURIComponent(obj[s]));  
            	}
            	return str.join("&");  
           	}
	    }).then(
	    	function successCallback(response) {
	    		var detail = response.data.dataList[0];
				$scope.orderDetail = {
					"orderNo" : detail.orderNo,
					"createTime" : detail.createTime,
					"poster" : detail.poster,
					"name" : detail.name,
					"dept" : detail.dept,
					"instrumentList": [],
					"sum" : ""
				};
	    		$http({
	    	        method: 'POST',
	    	        url: '../query.do',
	    	        data: { "queryID": "instrument.S001", "condition": [orderID] },  
	    	        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },  
	               	transformRequest: function(obj) {
	                	var str = [];
	                	for (var s in obj) {
	                    	str.push(encodeURIComponent(s) + "=" + encodeURIComponent(obj[s]));  
	                	}
	                	return str.join("&");  
	               	}
	    	    }).then(
	    	    	function successCallback(response) {
	    	    		$scope.orderDetail.instrumentList = response.data.dataList;
	    	    		var sum = 0;
	    	    		for (var i=0;i<response.data.dataList.length;i++) {
							sum += isNaN(response.data.dataList[i].fee) ? 0 : parseInt(response.data.dataList[i].fee);
	    	    		}
	    	    		$scope.orderDetail.sum = sum;
	    	        }, function errorCallback(response) {
	    	        	console.log(response);
	    	    });
	        }, function errorCallback(response) {
	        	console.log(response);
	    });
	};

	$scope.titleClick = function(status) {

		if (status == 0) {
			$scope.titleClass = [ {
				"li" : "w33 rline",
				"span" : "tabfouse"
			}, {
				"li" : "w33",
				"span" : ""
			}, {
				"li" : "w33",
				"span" : ""
			} ];
		} else if (status == 1) {
			$scope.titleClass = [ {
				"li" : "w33",
				"span" : ""
			}, {
				"li" : "w33 rline",
				"span" : "tabfouse"
			}, {
				"li" : "w33",
				"span" : ""
			} ];
		} else if (status == 2) {
			$scope.titleClass = [ {
				"li" : "w33",
				"span" : ""
			}, {
				"li" : "w33",
				"span" : ""
			}, {
				"li" : "w33 rline",
				"span" : "tabfouse"
			} ];
		}
	};
	
	query();
	$scope.titleClick(0);
});

app.controller('detail1Controller', function($scope, $http) {

	$scope.titleClick(0);
	pageState = {
		detail1 : true,
		detail2 : false,
		detail3 : false
	};
});
app.controller('detail2Controller', function($scope, $http) {

	$scope.titleClick(1);
	pageState = {
		detail1 : false,
		detail2 : true,
		detail3 : false
	};
});
app.controller('detail3Controller', function($scope, $http) {

	$scope.titleClick(2);
	pageState = {
		detail1 : false,
		detail2 : false,
		detail3 : true
	};
});

// 页面返回刷新
pushHistory();
setTimeout(function () {
	window.addEventListener("popstate", function (e) {
    	var href = window.location.href;
    	if (href.indexOf("order_detail.do#!/fee") > 0) {
    		if (pageState.detail3) {
    			window.location.href = 'order_list.html';
    		}
    	} else if (href.indexOf("order_detail.do#!/progress") > 0) {
    		if (pageState.detail2) {
    			window.location.href = 'order_list.html';
    		}
    	} else {
    		if (pageState.detail1) {
    			window.location.href = 'order_list.html';
    		}
    	}
    }, false);
}, 300);
function pushHistory() {
    var state = {
        title: "title",
        url: "#"
    };
    window.history.pushState(state, "title", "#");
}
